﻿using Lamex.Base;
using Lamex.SystemManager.BLL;
using Lamex.Common;
using Lamex.SystemManager.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.SystemManager.ucControls
{
   public class Globle
    {
        //public MainForm MainDialog;

        private string manageUser = "";
        public string gAppMsgboxTitle = string.Empty;   //程序的对话框标题
        public string gAppUnit = string.Empty; //单位名称
        public LicenseCheckResult LicenseResult = new LicenseCheckResult();//授权码检查的结果

        /// <summary>
        /// 登录用户信息
        /// </summary>
        //public TUserInfo UserInfo { get; set; }

        /// <summary>
        /// 用户具有的角色集合
        /// </summary>
        public List<RoleInfo> RoleList { get; set; }

        #region 基本操作函数

        /// <summary>
        /// Quits the application
        /// </summary>
        public void Quit()
        {
            Application.Exit();
        }

        /// <summary>
        /// Opens the help document.
        /// </summary>
        public void Help()
        {
            try
            {
                const string helpfile = "Help.chm";
                Process.Start(helpfile);
            }
            catch (Exception)
            {
                MessageBox.Show("文件打开失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 关于对话框信息
        /// </summary>
        public void About()
        {
            //AboutBox dlg = new AboutBox();
            //dlg.StartPosition = FormStartPosition.CenterScreen;
            //dlg.ShowDialog();
        }

        /// <summary>
        /// 检查用户的授权码，系统运行开始调用即可
        /// </summary>
        /// <returns></returns>
        public LicenseCheckResult CheckLicense()
        {
            LicenseCheckResult result = new LicenseCheckResult();
            //string license = MyConstants.License;
            //if (!string.IsNullOrEmpty(license))
            //{
            //    try
            //    {
            //        string decodeLicense = Base64Util.Decrypt(MD5Util.RemoveMD5Profix(license));
            //        string[] strArray = decodeLicense.Split('|');
            //        if (strArray.Length >= 4)
            //        {
            //            string componentType = strArray[0];
            //            if (componentType.ToLower() == "whc.security")
            //            {
            //                result.IsValided = true;
            //            }
            //            result.Username = strArray[1];
            //            result.CompanyName = strArray[2];
            //            try
            //            {
            //                result.DisplayCopyright = Convert.ToBoolean(strArray[3]);
            //            }
            //            catch
            //            {
            //                result.DisplayCopyright = true;
            //            }

            //            this.LicenseResult = result;//保存授权结果到变量中，方便调用
            //        }
            //    }
            //    catch
            //    {
            //    }
            //}

            return result;
        }
        #endregion

        /// <summary>
        /// 根据机构分类获取对应的图形序号
        /// </summary>
        /// <param name="category">机构分类</param>
        /// <returns></returns>
        public int GetImageIndex(string category)
        {
            int index = 0;
            if (category == OUCategoryEnum.公司.ToString())
            {
                index = 1;
            }
            else if (category == OUCategoryEnum.部门.ToString())
            {
                index = 2;
            }
            else if (category == OUCategoryEnum.工作组.ToString())
            {
                index = 3;
            }
            return index;
        }

        /// <summary>
        /// 判断当前用户具有某个角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public bool UserInRole(string roleName)
        {
            bool result = false;
            if (RoleList != null)
            {
                foreach (RoleInfo info in RoleList)
                {
                    if (info.JobName.Equals(roleName, StringComparison.OrdinalIgnoreCase))
                    {
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 判断当前管理员是否有权限管理相关机构节点
        /// </summary>
        /// <param name="companyId">待管理的机构公司ID</param>
        /// <returns></returns>
        //public bool DataCanManage(object companyId)
        //{
        //    bool result = false;
        //    if (UserInRole(RoleInfo.SuperAdminName))
        //    {
        //        result = true;
        //    }
        //    else if (UserInRole(RoleInfo.CompanyAdminName))
        //    {
        //        result = (UserInfo.Company_ID == companyId.ToString());
        //    }
        //    return result;
        //}

        /// <summary>
        /// 根据当前用户身份，获取对应的顶级机构管理节点。
        /// 如果是超级管理员，返回集团节点；如果是公司管理员，返回其公司节点
        /// </summary>
        /// <returns></returns>
        public tb_OrgStuctureInfo GetMyTopGroup(string Company_ID)
        {
            tb_OrgStuctureInfo groupInfo = null;
            //if (UserInRole("超级管理员"))
            if(true)
            {
                groupInfo = BaseFactory<tb_OrgStucture>.Instance.GetFirsttEntriyList().FirstOrDefault();//超级管理员取集团节点
            }
            else
            {
                groupInfo = BaseFactory<tb_OrgStucture>.Instance.GetEntriyList(" 1=1 and  Company_ID='" + Company_ID + "'").FirstOrDefault();//公司管理员取公司节点
            }
            return groupInfo;
        }
    }

   public class LicenseCheckResult
   {
       /// <summary>
       /// 是否验证通过
       /// </summary>
       public bool IsValided = false;

       /// <summary>
       /// 注册的用户名称
       /// </summary>
       public string Username = "";

       /// <summary>
       /// 注册的公司名称
       /// </summary>
       public string CompanyName = "";

       /// <summary>
       /// 是否显示授权信息
       /// </summary>
       public bool DisplayCopyright = true;
   }
}
